package com.fingard.dsp.bank.directbank.wrcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

public class WRCB01Transaction extends WRCB01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            //按自然月循环查询
            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                xmlWriter.writeStartElement("Message");
                xmlWriter.writeStartElement("Head");
                xmlWriter.writeElementString("TrsCode", "B2EActTrsQry");//交易码
                xmlWriter.writeElementString("ERPTrsTimestamp", p_req7516.reqHeader.transTime);//客户端时间
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeStartElement("List");
                xmlWriter.writeStartElement("Map");
                xmlWriter.writeElementString("requestPatches", "1");
                xmlWriter.writeElementString("startPatches", "1");
                xmlWriter.writeElementString("BeginDate", p_req7516.dateFrom.substring(0, 4) + "-" + p_req7516.dateFrom.substring(4, 6) + "-" + p_req7516.dateFrom.substring(6, 8));
                xmlWriter.writeElementString("EndDate", p_req7516.dateTo.substring(0, 4) + "-" + p_req7516.dateTo.substring(4, 6) + "-" + p_req7516.dateTo.substring(6, 8));
                xmlWriter.writeElementString("AcNo", p_req7516.actNum);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                XmlTextReader tmpBkReader;
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String returnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if (returnCode != null && returnCode.equals("000000")) {
                            String tmpReturnCode = tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnCode");
                            String tmpReturnMsg = tmpBkReader.getFirstText("/Message/Body/List/Map/ReturnMsg");
                            if (tmpReturnCode.equals("000000")) {
                                while (tmpBkReader.read("/Message/Body/List/Map/List/Map")) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    XElement tmpRecord = tmpBkReader.readedXElement;
                                    tmpRetDetail.actNum = p_req7516.actNum;//查询的账号
                                    tmpRetDetail.actArea = p_req7516.actArea;
                                    String cdFlag = tmpRecord.getSingleText("DCFlag");//借贷标记
                                    tmpRetDetail.cdFlag = cdFlag.equals("D") ? "1" : "2";
                                    tmpRetDetail.tranAmt = tmpRecord.getSingleText("Amount");//交易金额
                                    tmpRetDetail.tranDate = tmpRecord.getSingleText("TransDate");//？？？交易日期(格式不确定)
                                    tmpRetDetail.tranTime = tmpRecord.getSingleText("TransTime");//？？？交易时间(格式不确定)
                                    tmpRetDetail.cur = convertCurToATS(tmpRecord.getSingleText("Currency"));//币种
                                    tmpRetDetail.accBal = tmpRecord.getSingleText("Balance");//账户余额
//                                    tmpRetDetail.recAccNum= "";//对方账号（银行接口不返回）
                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            } else {
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpReturnMsg;
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = returnMsg;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);//下个月
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (
                Exception e)

        {
            e.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = "查询交易明细出现异常！";
            WriteBankLogLn("查询交易明细出现异常,异常信息：" + e.getMessage());
        }
        if ("-1".

                equals(tmpRespCode))

        {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0)

        {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空!");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
